import { CommonForm } from "@/pages/OrderModule/common/components/CommonForm";
import { CommonModal } from "@/pages/OrderModule/common/components/CommonModal";
import Typography from "@/pages/OrderModule/common/components/Typography";
import { Form, Modal, message } from "antd";
import { find } from 'lodash';
import OrderNumberCount from "../../components/OrderNumberCount";
import Api from "../../service";
import CommonLoading from "@/components/CommonLoading";
import { useState, useRef } from "react";
import OrderAreaRadioGroup from "../../components/OrderAreaRadioGroup1";

/** 获取运单号 */
function GetWaybillNumberModal(props) {
	const [showNumber, setShowNumber] = useState({});
	const [loading, setLoading] = useState(false);
	const filterIds = useRef(undefined);

	const { searchForm, selectKeys, orderList,  handleCancel, handleOk, item :{ isClear }= { } } = props;

	const [form] = Form.useForm();

	const onOk = async () => {
		setLoading(true);
		let res;
		if(isClear){
			res = await Api.clearLogisticsNoOp({ orderIdList: filterIds.current });
		}else{
			res = await Api.getLogisticsNoOp(filterIds.current);
		}
		const { msg, success, data } = res || {};
		if (success) {
			if(data?.msg){
        Modal.info({
          title:'清空运单号提示',
          content: data.msg
        })
      }else{
        message.success(msg);
      }
			handleOk();
		}
		setLoading(false);
	};

	const orderAreaRadioProps = {
		searchForm,
		selectKeys,
		orderList,
		states: [10, 20, 30],
		updateShowNumer(obj, ids) {
			filterIds.current = ids;
			setShowNumber(obj);
		},
    title: isClear ? '' : '仅限待审核、异常状态、待发货订单'
	};

	return (
		<CommonModal title={{ title: `${isClear ? '清空' : '获取'}运单号` }} open={true} width="530px" onOk={onOk} onCancel={handleCancel}>
			<CommonForm
				formProps={{
					form,
					size: "middle"
				}}
				columns={[
					{
						name: "radio-group",
						slot: <OrderAreaRadioGroup wrapStyle={{ marginTop: 16 }} {...orderAreaRadioProps} />
					},
					{
						block: (
							<OrderNumberCount
								data={[
									{ label: "待审核", value: showNumber[10], type: "warning" },
									{ label: "异常", value: showNumber[30], type: "error" },
									{ label: "待发货", value: showNumber[20] }
								]}
							/>
						)
					},
					{
						block: (
							<div style={{ marginTop: 24 }}>
								<Typography color="#333">{isClear ? '是否对上述订单清空运单号？' : '是否确认获取运单号？'}</Typography>
							</div>
						)
					},
					{
						block: <Typography color="#999">如果是京东云仓订单，则会通过店铺关联的云仓和事业部编码，获取云仓运单号</Typography>
					}
				]}
			></CommonForm>
			<CommonLoading loading={loading} />
		</CommonModal>
	);
}

export default GetWaybillNumberModal;
